/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hookedonplay.decoviewsample;

import com.hookedonplay.decoviewlib.DecoView;
import com.hookedonplay.decoviewlib.PointF;
import com.hookedonplay.decoviewlib.charts.DecoDrawEffect;
import com.hookedonplay.decoviewlib.charts.SeriesItem;
import com.hookedonplay.decoviewlib.events.DecoEvent;
import com.hookedonplay.decoviewlib.interpolator.AccelerateInterpolator;
import com.hookedonplay.decoviewlib.interpolator.BounceInterpolator;
import com.hookedonplay.decoviewsample.ResourceTable;
import com.hookedonplay.decoviewsample.Utility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.element.ShapeElement;

import java.security.SecureRandom;

public class Sample3Ability extends Ability {
    final float mSeriesMax = 50f;

    final private int COLOR_BLUE = RgbPalette.parse("#7B1FA2");
    final private int COLOR_PINK = RgbPalette.parse("#FBC02D");
    final private int COLOR_YELLOW = RgbPalette.parse("#FF5722");
    final private int COLOR_GREEN = RgbPalette.parse("#FFCDD2");
    final private int COLOR_EDGE = RgbPalette.parse("#009688");
    final private int COLOR_BACK = RgbPalette.parse("#F44366");
    final private int COLOR_NEUTRAL = RgbPalette.parse("#FFF9C4");

    final private int[] mColor = {
            COLOR_BLUE,
            COLOR_PINK,
            COLOR_YELLOW,
            COLOR_GREEN,
            COLOR_EDGE,
            COLOR_BACK,
            COLOR_NEUTRAL
    };
    private int[] mSeriesIndex = new int[7];
    private int mBackIndex;
    private boolean mFullCircle = true;
    private boolean mFlip = true;

    private DecoView decoView;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_sample_3);

        decoView = (DecoView) findComponentById(ResourceTable.Id_decoView);

        createTracks();
        setupEvents();
    }

    private void createTracks() {
        mFullCircle = !mFullCircle;
        mFlip = mFullCircle ? !mFlip : mFlip;

        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(32, 32, 32));
        decoView.setBackground(element);

        decoView.deleteAll();
        decoView.configureAngles(mFullCircle ? 360 : 180, mFlip ? 180 : 0);


        float widthLine = Utility.getDimension(14f, getApplicationContext());

        SeriesItem seriesBackItem = new SeriesItem.Builder(RgbPalette.parse("#11FFFFFF"))
                .setRange(0, mSeriesMax, mSeriesMax)
                .setLineWidth(widthLine * mSeriesIndex.length)
                .setInitialVisibility(false)
                .setCapRounded(false)
                .build();

        mBackIndex = decoView.addSeries(seriesBackItem);

        float inset = -((widthLine * (mSeriesIndex.length - 1)) / 2);
        for (int i = 0; i < mSeriesIndex.length; i++) {
            SeriesItem seriesItem = new SeriesItem.Builder(mColor[i])
                    .setRange(0, mSeriesMax, mSeriesMax)
                    .setLineWidth(widthLine)
                    .setInset(new PointF(inset, inset))
                    .setInitialVisibility(false)
                    .build();

            mSeriesIndex[i] = decoView.addSeries(seriesItem);

            inset += widthLine;
        }
    }

    private void setupEvents() {
        if (decoView == null || decoView.isEmpty()) {
            throw new IllegalStateException("Unable to add events to empty DecoView");
        }
        decoView.executeReset();

        decoView.addEvent(new DecoEvent.Builder(DecoEvent.EventType.EVENT_SHOW, true)
                .setIndex(mBackIndex)
                .setDelay(100)
                .setDuration(3000)
                .build());

        decoView.addEvent(new DecoEvent.Builder(DecoEvent.EventType.EVENT_HIDE, false)
                .setIndex(mBackIndex)
                .setDelay(18000)
                .setDuration(4000)
                .setListener(new DecoEvent.ExecuteEventListener() {
                    @Override
                    public void onEventStart(DecoEvent event) {

                    }

                    @Override
                    public void onEventEnd(DecoEvent event) {
                        createTracks();
                        setupEvents();
                    }
                })
                .build());

        SecureRandom random = new SecureRandom();
        for (int i = 0; i < mSeriesIndex.length; i++) {
            int index = mSeriesIndex[i];

            decoView.addEvent(new DecoEvent.Builder(DecoDrawEffect.EffectType.EFFECT_SPIRAL_OUT_FILL)
                    .setIndex(mSeriesIndex[i])
                    .setDelay(500 + i * 250)
                    .setDuration(2500)
                    .build());

            decoView.addEvent(new DecoEvent.Builder(random.nextInt((int) mSeriesMax))
                    .setIndex(index)
                    .setDelay(5000 + i * 750)
                    .build());

            decoView.addEvent(new DecoEvent.Builder(random.nextInt((int) mSeriesMax / 2))
                    .setIndex(index)
                    .setDelay(10000 + i * 500)
                    .setColor(RgbPalette.parse("#FF555555"))
                    .setDuration(2000)
                    .build());

            decoView.addEvent(new DecoEvent.Builder(mSeriesMax)
                    .setIndex(index)
                    .setDelay(15000)
                    .setDuration(2000)
                    .setInterpolator(new BounceInterpolator())
                    .setColor(mColor[i])
                    .build());

            decoView.addEvent(new DecoEvent.Builder(0)
                    .setIndex(index)
                    .setDelay(17500 + i * 200)
                    .setDuration(1000)
                    .setInterpolator(new AccelerateInterpolator()).build());

            decoView.addEvent(new DecoEvent.Builder(DecoDrawEffect.EffectType.EFFECT_SPIRAL_IN)
                    .setIndex(mSeriesIndex[i])
                    .setDelay(18500 + i * 200)
                    .setDuration(2000)
                    .setEffectRotations(3)
                    .build());
        }
    }
}